﻿using System;
using System.Collections.Generic;
using System.Data;
using AMS.DataAccess.BO;
using AMS.DataAccess.Common;
using AMS.DataAccess.Sql;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Trademark.Interfaces;

namespace AMS.DataAccess.Trademark
{
    public class CaseContactorManager : ICaseContactorManager
    {
        public List<CaseContactorInfo> GetCaseContactors(CallContext context, int caseId)
        {
             return GetCaseContactors(context, caseId, false);
        }

        public List<CaseContactorInfo> GetCaseContactors(CallContext context, int caseId, bool onlyFirst)
        {
            return GetCaseContactors(context, caseId, 0, onlyFirst);
        }

        public CaseContactorInfo GetCaseContactor(CallContext context, int caseId, int contactorId)
        {
            List<CaseContactorInfo> caseContactors = GetCaseContactors(context, caseId, contactorId, false);
            if (caseContactors != null && caseContactors.Count > 0)
            {
                return caseContactors[0];
            }
            else
            {
                return null;
            }
        }

        private List<CaseContactorInfo> GetCaseContactors(CallContext context, int caseId, int contactorId, bool onlyFirst)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT c.*, cc.CaseID, cc.IsFirst");
            sql.Add("FROM");
            sql.Add("TT_Contactor cc,");
            sql.Add("TC_Contactor c");
            sql.Add("WHERE");
            sql.Add("    c.AID = cc.ContactorID");
            sql.Add("    AND ", new SqlCondition("CaseID", caseId));
            if (contactorId != 0)
            {
                sql.Add("    AND ", new SqlCondition("ContactorID", contactorId));
            }

            if (onlyFirst)
            {
                sql.Add("    AND IsFirst = 1");
            }

            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0] == null)
            {
                return null;
            }

            List<CaseContactorInfo> caseContactors = BusinessObjectMapping.DataTableToBOList<CaseContactorInfo>(ds.Tables[0]);
            return caseContactors;
        }

        public bool DeleteCaseContactor(CallContext context, int caseId, int contactorId)
        {
            SqlDelete delete = new SqlDelete("TT_Contactor");
            delete.AddCondition(new SqlCondition("CaseID", caseId));
            delete.AddCondition(new SqlCondition("ContactorID", contactorId));

            AMS.DataAccess.SqlDebugger.WriteLine(delete);

            return 0 < SqlDBHelper.ExecuteSql(delete.ToString());
        }

        public void AddCaseContactor(CallContext context, CaseContactorInfo contactorInfo)
        {
            SqlInsert insert = new SqlInsert("TT_Contactor");
            insert.AddColumn("CaseID", contactorInfo.CaseID);
            insert.AddColumn("ContactorID", contactorInfo.ContactorID);

            AMS.DataAccess.SqlDebugger.WriteLine(insert);

            SqlDBHelper.ExecuteSql(insert.ToString());
        }
    }
}
